package com.ad.backstage.common.exception;

import com.ad.payload.enums.ResultCode;
import com.ad.payload.model.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.validation.ConstraintViolationException;
import javax.validation.UnexpectedTypeException;
import java.sql.SQLSyntaxErrorException;

/**
 * 全局异常处理
 *
 * @Author: XYJ
 * @CreateDate: 2019/7/5
 */

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResultBean illegalArgumentException(IllegalArgumentException e) {
        log.error("参数非法异常={}", e.getMessage(), e);
        return ResultBean.build(ResultCode.FAILED.getCode(), e.getMessage());
    }



    /**
     * @param e
     * @return Result
     * @NotBlack 錯誤
     * @author WFZ
     * @date 2019/5/8 19:47
     */
    @ExceptionHandler(value = UnexpectedTypeException.class)
    @ResponseBody
    public ResultBean methodArgumentNotValidHandler(UnexpectedTypeException e) {
        return ResultBean.build(ResultCode.FAILED.getCode(), e.getMessage());
    }

    /**
     * 业务异常.
     *
     * @param e the e
     * @return the Response
     */
    @ExceptionHandler(BackStageRuntimeException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResultBean businessException(BackStageRuntimeException e) {
        log.error("业务异常={}", e.getMessage());
        return ResultBean.build(ResultCode.FAILED.getCode(), e.getMessage());
    }

    /**
     * 参数类型转换错误 （使用post请求）
     *
     * @param e 错误
     * @return 错误信息
     */
    @ExceptionHandler({HttpMessageConversionException.class})
    public ResultBean parameterTypeException(HttpMessageConversionException e) {
        log.error("post类型转换异常={}" + e.getMessage());
        return ResultBean.build(ResultCode.FAILED.getCode(), e.getMessage());

    }


    /**
     * 参数类型转换错误（使用对象接收的get请求）
     *
     * @param exception 错误
     * @return 错误信息
     */
    @ExceptionHandler(BindException.class)
    public ResultBean parameterTypeException(BindException exception) {
        log.error("get类型转换异常={}" + exception.getMessage());
        return ResultBean.build(ResultCode.FAILED.getCode(), exception.getMessage());

    }


    /**
     * 参数类型转换错误 （使用单个字段接收的get请求）
     *
     * @param exception 错误
     * @return 错误信息
     */
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public ResultBean parameterTypeException(MethodArgumentTypeMismatchException exception) {
        log.error("单个字段接收的get请求类型转换异常={}" + exception.getMessage());
        return ResultBean.build(ResultCode.FAILED.getCode(), exception.getMessage());

    }

    /**
     * 参数类型转换错误 （使用单个字段接收的get请求）
     *
     * @param exception 错误
     * @return 错误信息
     */
    @ExceptionHandler(NumberFormatException.class)
    public ResultBean parameterTypeException(NumberFormatException exception) {
        log.error("类型转换异常={}" + exception.getMessage());
        return ResultBean.build(ResultCode.FAILED.getCode(), exception.getMessage());
    }

    /**
     * 缺少参数异常
     *
     * @author WFZ
     * @date 2019/5/17 10:54
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResultBean parameterTypeException(MissingServletRequestParameterException exception) {
        log.error("缺少参数异常={}" + exception.getMessage());
        return ResultBean.build(ResultCode.FAILED.getCode(), exception.getMessage());
    }

    /**
     * 请求方法异常
     *
     * @author WFZ
     * @date 2019/5/17 10:54
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResultBean parameterTypeException(HttpRequestMethodNotSupportedException exception) {
        log.error("请求方法异常={}" + exception.getMessage());
        return ResultBean.build(ResultCode.FAILED.getCode(), exception.getMessage());
    }

    /**
     * SQL异常
     *
     * @author WFZ
     * @date 2019/5/17 10:54
     */
    @ExceptionHandler(SQLSyntaxErrorException.class)
    public ResultBean parameterTypeException(SQLSyntaxErrorException exception) {
        log.error("SQL异常={}" + exception.getMessage());
        return ResultBean.build(ResultCode.FAILED.getCode(), exception.getMessage());
    }

    /**
     * 全局异常
     *
     * @param e the e
     * @return the Response
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResultBean exception(Exception e) {
        log.info("保存全局异常信息 ex={}", e.getMessage(), e);
        return ResultBean.build(ResultCode.FAILED.getCode(), "系统忙，请联系管理员");
    }


    /**
     * spring validate 异常
     *
     * @param e the e
     * @return the Response
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResultBean methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        log.info("spring validate 异常", e.getMessage(), e);
        return ResultBean.build(ResultCode.FAILED.getCode(), e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }



    /**
     * spring validate 异常
     * */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ResultBean validationExceptionHandler(ConstraintViolationException e){
        log.info("validate ex={}", e.getMessage(), e);
        return ResultBean.build(ResultCode.FAILED.getCode(), e.getMessage());
    }
}
